﻿using System;
using System.Linq;


using EDTS.Model.Orgnizations;
using EDTS.Model.Weixin;
namespace EDTS.Data.Orgnizations
{
    public class UnitCommand
    {
        public static UnitModel GetModel(Guid guid,bool getapp=false)
        {
            if (guid != Guid.Empty)
            {
                using (DBCentre db = new DBCentre())
                {
                    var d = db.DataContext.Units.FirstOrDefault(p => p.Guid == guid);
                    if (d != null)
                    {
                        var ret = Convertor.ConvertTo<UnitModel, Unit>(d);
                        if (getapp && ret.WxAppID > 0)
                            ret.WxApp = Convertor.ConvertTo<WxAppModel, WxApp>(d.WxApp);
                        return ret;
                    }
                }
            }
            return null;
        }

        private static void SaveCheck(UnitModel model, DBCentre db)
        {
            if (string.IsNullOrEmpty(model.UnitName))
                throw new Utils.IException("请输入单位名称");
            if (string.IsNullOrEmpty(model.Descript))
                throw new Utils.IException("请输入单位的详细描述");
            if (model.WxAppID==0)
                throw new Utils.IException("请提供绑定的微信企业号");
            if (db != null)
            {
                if (!db.DataContext.WxApps.Any(p => p.ID == model.WxAppID))
                    throw new Utils.IException("无效的微信企业号");
                if (db.DataContext.Units.Any(p=>p.UnitName==model.UnitName && p.Guid!=model.Guid))
                    throw new Utils.IException("单位名称重复");
            }
        }

        public static void CreateNew(UnitModel model)
        {
            if (model != null)
            {
                using (DBCentre db = new DBCentre())
                {
                    model.CreatedOn = DateTime.Now;
                    model.Guid = Guid.NewGuid();
                    model.Status = (int)Model.Status.Active;

                    SaveCheck(model, db);
                    var d = Convertor.ConvertFrom<Unit, UnitModel>(model);
                    db.DataContext.Units.Add(d);
                    db.DataContext.SaveChanges();
                }
            }
        }

        public static void Delete(Guid guid)
        {
            if (guid != Guid.Empty)
            {
                using (DBCentre db = new DBCentre())
                {
                    db.Delete<Unit>(p => p.Guid == guid);
                    db.DataContext.SaveChanges();
                }
            }
        }

        public static void AddWxDepID(Guid guid, int depid)
        {
            if (guid != Guid.Empty)
            {
                using (DBCentre db = new DBCentre())
                {
                    var d = db.DataContext.Units.FirstOrDefault(p => p.Guid == guid);
                    d.WeixinDepartmentID = depid;
                    db.DataContext.SaveChanges();
                }
            }
        }
    }
}
